﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;
using System.Threading;
using System.Configuration;
using XChip_Common;
using XChipTool.Enties;
using BinAnalysisHelp;

namespace XChipTool
{
    public partial class XChip_Lite_DNR : DevExpress.XtraEditors.XtraUserControl
    {

        public  SerialPort sPortTemp;
        private bool isRead = false;
        private KeyEventArgs keyEnter = new KeyEventArgs(Keys.Enter);//用来手动触发文本框下发配置
        private bool isWrite = false;
        List<string> listfiltration = new List<string> { "scale_sig", "slope", "offset" };
        private addressregisterManager aBll;
        private List<addressregister> XChipDnr;
        private List<addressregister> XChipSnr;
        //记录是否使用USB设备读写
        public bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;
        public XChip_Lite_DNR()
        {
            InitializeComponent();
        }
        public XChip_Lite_DNR(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
            sPortTemp = _sPortTemp;
            aBll = _aBll;
            XChipSnr = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "SNR"));

            XChipDnr = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "DNR" || c.Classify == "Common"));

            InitializeComponent();
        }

        private long GetDNRRegister(string nameType)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return -1;
            }
            if (XChipDnr.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
                return SerialSend.ReadRegister(Convert.ToUInt32(XChipDnr.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), sPortTemp);
            else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1; }
        }
    
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            isRead = true;
            WaitBefore.Start("读取中", "正在加载中", 10);
           

            List<string> NameList = new List<string>();

            for (int i = 0; i < XChipDnr.Count; i++)
            {
                if (XChipDnr[i].NameType.Contains("Parn_En"))
                {
                    
                }
                var type = AppHelper.NameTypeToString(XChipDnr[i].NameType.ToLower());
                //加上单选框的_en
                if (XChipDnr[i].ControlType == "1")
                {
                    type = XChipDnr[i].NameType;
                }
                var col = this.Controls.Find(type, true);

                foreach (var item in col)
                {
                    if (NameList.Exists(c => c == (type)))
                    {
                        continue;
                    }
                    else
                    {

                        if (item is TextEdit)
                        {

                            var nList = XChipDnr.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            if (nList.Count == 0)
                            {
                                nList = XChipDnr.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                            }
                            if (nList.Count == 1)
                            {
                                var Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                                item.Text = BitsHelp.GetBitsToUint(nList[0].Bits, nList[0].Hex, Val).ToString("X");
                            }
                            else
                            {
                                string Val = string.Empty;
                                //这里默认为是有多个寄存器  一个文本框的情况进行处理
                                if (nList.Exists(c => c.NameType.ToLower().Contains("_h")))
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_h")).Address, 16), sPortTemp).ToString("X2");
                                }
                                if (nList.Exists(c => c.NameType.ToLower().Contains("_m0")))
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m0")).Address, 16), sPortTemp).ToString("X2");
                                }
                                if (nList.Exists(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")))
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                }
                                if (string.IsNullOrEmpty(Val))
                                {
                                    nList = XChipDnr.Where(c => c.NameType.ToLower() == (item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                                    if (nList.Count() == 1)
                                    {
                                        Val += SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                                    }
                                }
                                item.Text = Val;
                            }
                        }
                        else if (item is CheckEdit)
                        {
                            var nList = XChipDnr.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                            if (nList.Count == 1)
                            {
                                var Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                                (item as CheckEdit).Checked = BitsHelp.GetBitsCheckedToBool(nList[0].Bits, nList[0].Hex, Val) == 0;
                            }
                        }


                    }
                }
                NameList.Add(type);
            }

            if (((GetDNRRegister("Px_Wt_Man_en")& 0x4) == 0x4) && ((GetDNRRegister("Px_Wt_Man3_en") & 0x8) == 0x8))
            {
                Px_Wt_Man_en.Checked = true;
            }
            else
            {
                Px_Wt_Man_en.Checked = false;

            }
            //bit1置0才是勾选
            if ((GetDNRRegister("Parn_en") & 0x2) == 0x2)
            {
                Parn_en.Checked = false;
            }
            else
            {
                Parn_en.Checked = true;
            }
            isRead = false;
            WaitBefore.Close();

            #region 勾选框判断逻辑
           
            if (Parn_en.CheckState == CheckState.Checked)
            {
                ThreeDParnEnShowHide(true);
                RawGslAndRawRbslShowHide(false);
            }
            else
            {
                ThreeDParnEnShowHide(false);
                RawGslAndRawRbslShowHide(true);
            }

            if (Px_Wt_Man_en.CheckState == CheckState.Checked) //这个是勾选下发值
            {
               
                PxWtManEnShowHide(false);
                Move_Px_Wt_Cal_en.Enabled = true;
                Still_Px_Wt_Cal_en.Enabled = true;

                MovePxWtCalEnShowHide(Move_Px_Wt_Cal_en.Checked);
                StillPxWtCalEnShowHide(Still_Px_Wt_Cal_en.Checked);

            }
            else
            {
                PxWtManEnShowHide(true);
                Move_Px_Wt_Cal_en.Enabled = false;
                Still_Px_Wt_Cal_en.Enabled = false;
                MovePxWtCalEnShowHide(false);
                StillPxWtCalEnShowHide(false);
                Cur_Move_Px_Wt.Enabled = false;
                Cur_Still_Px_Wt.Enabled = false;

            }

            //if (lap_enhancement_en.CheckState == CheckState.Checked)
            //{
            //    lap_enhancement_Enable(true);
            //    if (soft_thr_en.CheckState == CheckState.Checked)
            //    {
            //        soft_thr_Enable(true);
            //        SetTxtSlopeMan_Enable(soft_thr_slope_man_en.Checked);
            //        SetTxtOffsetMan_Enable(soft_thr_offset_man_en.Checked);
            //        soft_thr_offset_man_en.Enabled = true;
            //        soft_thr_slope_man_en.Enabled = true;
            //        if (soft_thr_offset_man_en.Checked)
            //        {
            //            soft_thr_offset_man.Enabled = true;
            //        }
            //        if (soft_thr_slope_man_en.Checked)
            //        {
            //            soft_thr_slope_man.Enabled = true;
            //        }
            //    }
            //    else
            //    {
            //        soft_thr_Enable(false);
            //        soft_thr_offset_man_en.Enabled = false;
            //        soft_thr_slope_man_en.Enabled = false;
            //        soft_thr_slope_man.Enabled = false;
            //        soft_thr_offset_man.Enabled = false;
            //    }

            //}
            //else
            //{
            //    lap_enhancement_Enable(false);
            //}

            SetMotionPixel();
            SetStillPixel();
            #endregion
            //if (!sPortTemp.IsOpen)
            //{
            //    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
            //    return;
            //}
            //WaitBefore.Start("读取中", "正在加载中", 10);
            //isRead = true;
            //if (((GetDNRRegister("PxWtManEn")) & 0x0c) == 0x0c)
            //{
            //    Px_Wt_Man_En.CheckState = CheckState.Checked;
            //    PxWtManEnShowHide(false);
            //    Move_Px_Wt_Cal_En.Enabled = true;
            //    Still_Px_Wt_Cal_En.Enabled = true;                
            //}
            //else
            //{
            //    Px_Wt_Man_En.CheckState = CheckState.Unchecked;
            //    PxWtManEnShowHide(true);
            //    Move_Px_Wt_Cal_En.Enabled = false;
            //    Still_Px_Wt_Cal_En.Enabled = false;
            //    MovePxWtCalEnShowHide(false);
            //    StillPxWtCalEnShowHide(false);
            //}
            //        if ((GetDNRRegister("MovePxWtCalEn")) == 1)
            //{
            //    Move_Px_Wt_Cal_En.CheckState = CheckState.Checked;
            //    if (Px_Wt_Man_En.CheckState == CheckState.Checked)
            //    {
            //        MovePxWtCalEnShowHide(true);
            //    }
            //    else {
            //        MovePxWtCalEnShowHide(false);
            //    }           
            //}
            //else
            //{
            //    Move_Px_Wt_Cal_En.CheckState = CheckState.Unchecked;
            //    MovePxWtCalEnShowHide(false);
            //}


            //if ((GetDNRRegister("StillPxWtCalEn")) == 1)
            //{
            //    Still_Px_Wt_Cal_En.CheckState = CheckState.Checked;
            //    if (Px_Wt_Man_En.CheckState == CheckState.Checked)
            //    {
            //        StillPxWtCalEnShowHide(true);
            //    }
            //    else
            //    {
            //        StillPxWtCalEnShowHide(false);
            //    }
            //}
            //else
            //{
            //    Still_Px_Wt_Cal_En.CheckState = CheckState.Unchecked;
            //    StillPxWtCalEnShowHide(false);
            //}
            //if ((GetDNRRegister("ParnEn") & 0x01) == 1)
            //{
            //    Parn_En.CheckState = CheckState.Checked;
            //    ThreeDParnEnShowHide(true);
            //    RawGslAndRawRbslShowHide(false);
            //}
            //else
            //{
            //    Parn_En.CheckState = CheckState.Unchecked;
            //    ThreeDParnEnShowHide(false);
            //    RawGslAndRawRbslShowHide(true);
            //}

            //if ((GetDNRRegister("UvDNSManualEn") >> 7 & 0x01) == 1)
            //{
            //    Uv_DNR_Manual_En.CheckState = CheckState.Checked;
            //    UvDnsManualShowHide(false);        
            //}
            //else
            //{
            //    Uv_DNR_Manual_En.CheckState = CheckState.Unchecked;
            //    UvDnsManualShowHide(true);                
            //}
            //string dataTag = Thr_offset_man_en.Tag.ToString();
            //var tempChk = GetDNRRegister(dataTag);
            //Thr_offset_man_en.Checked = (tempChk & 0x02) == 0x02;
            //Thr_slope_man_en.Checked = (tempChk & 0x01) == 0x01;

            //Wt_Pedestal.Text = GetDNRRegister("WtPedestal").ToString("X2");
            //Max_Move_Px_Wt.Text = GetDNRRegister("MaxMovePxWt").ToString("X2");
            //Move_Diff_Ratio.Text = GetDNRRegister("MoveDiffRatio").ToString("X2");
            //Cur_Still_Px_Wt.Text = GetDNRRegister("CurStillPxWt").ToString("X2");
            //Cur_Move_Px_Wt.Text = GetDNRRegister("CurMovePxWt").ToString("X2");
            //Uv_DNR_Manual.Text = GetDNRRegister("UvDnsManual").ToString("X2");

            //InitDns();

            //Lap_enhance_scale.Text = GetDNRRegister(Lap_enhance_scale.Tag.ToString()).ToString("X2");
            //Med_filter_sclae.Text = GetDNRRegister(Med_filter_sclae.Tag.ToString()).ToString("X2");
            //thr_offset_man.Text = GetDNRRegister(thr_offset_man.Tag.ToString()).ToString("X2");
            //thr_slope_man.Text = GetDNRRegister(thr_slope_man.Tag.ToString()).ToString("X2");

            NameList = new List<string>();

            for (int i = 0; i < XChipSnr.Count; i++)
            {
                if (XChipSnr[i].NameType.Contains("enh_en"))
                {

                }
                var type = AppHelper.NameTypeToString(XChipSnr[i].NameType);
                if (NameList.Exists(c => c.ToLower() == (type)))
                {
                    continue;
                }
                //加上单选框的_en
                if (XChipSnr[i].ControlType == "1")
                {
                    type = XChipSnr[i].NameType;
                }
                var col = Controls_Find(type);

                foreach (var item in col)
                {
                    if (item is TextEdit)
                    {

                        var nList = XChipSnr.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        if (nList.Count == 0)
                        {
                            nList = XChipSnr.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        }
                        if (nList.Count == 1)
                        {
                            var Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                            item.Text = BitsHelp.GetBitsToUint(nList[0].Bits, nList[0].Hex, Val).ToString("X");
                        }
                        else
                        {
                            string Val = string.Empty;
                            //这里默认为是有多个寄存器  一个文本框的情况进行处理
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_h")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_h")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_m2")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m0")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_m1")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m1")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_m0")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m2")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_l")))
                            {
                                if (nList.Exists(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")))
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                }
                                else
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Last(c => c.NameType.ToLower().Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                }
                            }

                            item.Text = Val;
                        }
                    }
                    else if (item is CheckEdit)
                    {
                        var nList = XChipSnr.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                        if (nList.Count == 1)
                        {
                            var Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                            (item as CheckEdit).Checked = BitsHelp.GetBitsCheckedToBool(nList[0].Bits, nList[0].Hex, Val) == 0;
                        }
                    }
                    NameList.Add(item.Name);

                }

                //NameList.Add(type);
            }


            EnShowHide();
            isRead = false;
            WaitBefore.Close();
        }
        private Control[] Controls_Find(string str)
        {
            //var t =   this.Controls.Find(type, true);
            List<Control> listc = new List<Control>();
            System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            for (int i = 0; i < fieldInfo.Length; i++)
            {
                if (fieldInfo[i].Name.ToLower().Contains(str.ToLower()))
                {
                    listc.AddRange(this.Controls.Find(fieldInfo[i].Name, true));
                    //break;
                }
            }
            return listc.ToArray();
        }
        private void InitDns() {
    
            Raw_Sigma0.Text = GetDNRRegister("RawSigma0").ToString("X2");
            Raw_Sigma1.Text = GetDNRRegister("RawSigma1").ToString("X2");
            Raw_Sigma2.Text = GetDNRRegister("RawSigma2").ToString("X2");
            Raw_Sigma3.Text = GetDNRRegister("RawSigma3").ToString("X2");
            Raw_Sigma4.Text = GetDNRRegister("RawSigma4").ToString("X2");
            Raw_Sigma5.Text = GetDNRRegister("RawSigma5").ToString("X2");
     
            Yuv_Sigma0.Text = GetDNRRegister("YuvSigma0").ToString("X2");
            Yuv_Sigma1.Text = GetDNRRegister("YuvSigma1").ToString("X2");
            Yuv_Sigma2.Text = GetDNRRegister("YuvSigma2").ToString("X2");
            Yuv_Sigma3.Text = GetDNRRegister("YuvSigma3").ToString("X2");
            Yuv_Sigma4.Text = GetDNRRegister("YuvSigma4").ToString("X2");
            Yuv_Sigma5.Text = GetDNRRegister("YuvSigma5").ToString("X2");
   
            Ysl0.Text = GetDNRRegister("Ysl0").ToString("X2");
            Ysl1.Text = GetDNRRegister("Ysl1").ToString("X2");
            Ysl2.Text = GetDNRRegister("Ysl2").ToString("X2");
            Ysl3.Text = GetDNRRegister("Ysl3").ToString("X2");
            Ysl4.Text = GetDNRRegister("Ysl4").ToString("X2");
            Ysl5.Text = GetDNRRegister("Ysl5").ToString("X2");


            Uv_Dnr0.Text = GetDNRRegister("UvDns0").ToString("X2");
            Uv_Dnr1.Text = GetDNRRegister("UvDns1").ToString("X2");
            Uv_Dnr2.Text = GetDNRRegister("UvDns2").ToString("X2");
            Uv_Dnr3.Text = GetDNRRegister("UvDns3").ToString("X2");
            Uv_Dnr4.Text = GetDNRRegister("UvDns4").ToString("X2");
            Uv_Dnr5.Text = GetDNRRegister("UvDns5").ToString("X2");
            Uv_Dnr6.Text = GetDNRRegister("UvDns6").ToString("X2");
            Uv_Dnr7.Text = GetDNRRegister("UvDns7").ToString("X2");
            Uv_Dnr8.Text = GetDNRRegister("UvDns8").ToString("X2");
            Uv_Dnr9.Text = GetDNRRegister("UvDns9").ToString("X2");
 
            Raw_Gsl0.Text = GetDNRRegister("RawGsl0").ToString("X2");
            Raw_Gsl1.Text = GetDNRRegister("RawGsl1").ToString("X2");
            Raw_Gsl2.Text = GetDNRRegister("RawGsl2").ToString("X2");
            Raw_Gsl3.Text = GetDNRRegister("RawGsl3").ToString("X2");
            Raw_Gsl4.Text = GetDNRRegister("RawGsl4").ToString("X2");
            Raw_Gsl5.Text = GetDNRRegister("RawGsl5").ToString("X2");
            Raw_Gsl6.Text = GetDNRRegister("RawGsl6").ToString("X2");
            Raw_Gsl7.Text = GetDNRRegister("RawGsl7").ToString("X2");
            Raw_Gsl8.Text = GetDNRRegister("RawGsl8").ToString("X2");
            Raw_Gsl9.Text = GetDNRRegister("RawGsl9").ToString("X2");

            Raw_Rbsl9.Text = SerialSend.ReadRegister(Convert.ToUInt32(XChipDnr.Where(c => c.NameType.ToLower() == "RawRbsl9".ToLower()).First().Address, 16), sPortTemp).ToString("X2");

            Raw_Rbsl0.Text = GetDNRRegister("RawRbsl0").ToString("X2");
            Raw_Rbsl1.Text = GetDNRRegister("RawRbsl1").ToString("X2");
            Raw_Rbsl2.Text = GetDNRRegister("RawRbsl2").ToString("X2");
            Raw_Rbsl3.Text = GetDNRRegister("RawRbsl3").ToString("X2");
            Raw_Rbsl4.Text = GetDNRRegister("RawRbsl4").ToString("X2");
            Raw_Rbsl5.Text = GetDNRRegister("RawRbsl5").ToString("X2");
            Raw_Rbsl6.Text = GetDNRRegister("RawRbsl6").ToString("X2");
            Raw_Rbsl7.Text = GetDNRRegister("RawRbsl7").ToString("X2");
            Raw_Rbsl8.Text = GetDNRRegister("RawRbsl8").ToString("X2");
            Raw_Rbsl9.Text = GetDNRRegister("RawRbsl9").ToString("X2");
     
            //thr_slope_1x.Text = GetDNRRegister("thr_slope_1x").ToString("X2");
            //thr_slope_2x.Text = GetDNRRegister("thr_slope_2x").ToString("X2");
            //thr_slope_4x.Text = GetDNRRegister("thr_slope_4x").ToString("X2");
            //thr_slope_8x.Text = GetDNRRegister("thr_slope_8x").ToString("X2");
            //thr_slope_16x.Text = GetDNRRegister("thr_slope_16x").ToString("X2");
            //thr_slope_32x.Text = GetDNRRegister("thr_slope_32x").ToString("X2");
            //thr_slope_64x.Text = GetDNRRegister("thr_slope_64x").ToString("X2");
            //thr_slope_128x.Text = GetDNRRegister("thr_slope_128x").ToString("X2");
            //thr_slope_256x.Text = GetDNRRegister("thr_slope_256x").ToString("X2");
            //thr_offset_1x.Text = GetDNRRegister("thr_offset_1x").ToString("X2");
            //thr_offset_2x.Text = GetDNRRegister("thr_offset_2x").ToString("X2");
            //thr_offset_4x.Text = GetDNRRegister("thr_offset_4x").ToString("X2");
            //thr_offset_8x.Text = GetDNRRegister("thr_offset_8x").ToString("X2");
            //thr_offset_16x.Text = GetDNRRegister("thr_offset_16x").ToString("X2");
            //thr_offset_32x.Text = GetDNRRegister("thr_offset_32x").ToString("X2");
            //thr_offset_64x.Text = GetDNRRegister("thr_offset_64x").ToString("X2");
            //thr_offset_128x.Text = GetDNRRegister("thr_offset_128x").ToString("X2");
            //thr_offset_256x.Text = GetDNRRegister("thr_offset_256x").ToString("X2");

  
            Move_Dnr_Adj_Gain0.Text = GetDNRRegister("MoveDnsAdjGain0").ToString("X2");
            Move_Dnr_Adj_Gain1.Text = GetDNRRegister("MoveDnsAdjGain1").ToString("X2");
            Move_Dnr_Adj_Gain2.Text = GetDNRRegister("MoveDnsAdjGain2").ToString("X2");
            Move_Dnr_Adj_Gain3.Text = GetDNRRegister("MoveDnsAdjGain3").ToString("X2");
            Move_Dnr_Adj_Gain4.Text = GetDNRRegister("MoveDnsAdjGain4").ToString("X2");
            Move_Dnr_Adj_Gain5.Text = GetDNRRegister("MoveDnsAdjGain5").ToString("X2");
            Move_Dnr_Adj_Gain6.Text = GetDNRRegister("MoveDnsAdjGain6").ToString("X2");
            Move_Dnr_Adj_Gain7.Text = GetDNRRegister("MoveDnsAdjGain7").ToString("X2");
            Move_Dnr_Adj_Gain8.Text = GetDNRRegister("MoveDnsAdjGain8").ToString("X2");
   
            Still_Dnr_Adj_Gain0.Text = GetDNRRegister("StillDnsAdjGain0").ToString("X2");
            Still_Dnr_Adj_Gain1.Text = GetDNRRegister("StillDnsAdjGain1").ToString("X2");
            Still_Dnr_Adj_Gain2.Text = GetDNRRegister("StillDnsAdjGain2").ToString("X2");
            Still_Dnr_Adj_Gain3.Text = GetDNRRegister("StillDnsAdjGain3").ToString("X2");
            Still_Dnr_Adj_Gain4.Text = GetDNRRegister("StillDnsAdjGain4").ToString("X2");
            Still_Dnr_Adj_Gain5.Text = GetDNRRegister("StillDnsAdjGain5").ToString("X2");
            Still_Dnr_Adj_Gain6.Text = GetDNRRegister("StillDnsAdjGain6").ToString("X2");
            Still_Dnr_Adj_Gain7.Text = GetDNRRegister("StillDnsAdjGain7").ToString("X2");
            Still_Dnr_Adj_Gain8.Text = GetDNRRegister("StillDnsAdjGain8").ToString("X2");

            Cur_Move_Px_Wt_Gain0.Text = GetDNRRegister("CurMovePxWtGain0").ToString("X2");
            Cur_Move_Px_Wt_Gain1.Text = GetDNRRegister("CurMovePxWtGain1").ToString("X2");
            Cur_Move_Px_Wt_Gain2.Text = GetDNRRegister("CurMovePxWtGain2").ToString("X2");
            Cur_Move_Px_Wt_Gain3.Text = GetDNRRegister("CurMovePxWtGain3").ToString("X2");
            Cur_Move_Px_Wt_Gain4.Text = GetDNRRegister("CurMovePxWtGain4").ToString("X2");
            Cur_Move_Px_Wt_Gain5.Text = GetDNRRegister("CurMovePxWtGain5").ToString("X2");
            Cur_Move_Px_Wt_Gain6.Text = GetDNRRegister("CurMovePxWtGain6").ToString("X2");
            Cur_Move_Px_Wt_Gain7.Text = GetDNRRegister("CurMovePxWtGain7").ToString("X2");
            Cur_Move_Px_Wt_Gain8.Text = GetDNRRegister("CurMovePxWtGain8").ToString("X2");
   
            Cur_Still_Px_Wt_Gain0.Text = GetDNRRegister("CurStillPxWtGain0").ToString("X2");
            Cur_Still_Px_Wt_Gain1.Text = GetDNRRegister("CurStillPxWtGain1").ToString("X2");
            Cur_Still_Px_Wt_Gain2.Text = GetDNRRegister("CurStillPxWtGain2").ToString("X2");
            Cur_Still_Px_Wt_Gain3.Text = GetDNRRegister("CurStillPxWtGain3").ToString("X2");
            Cur_Still_Px_Wt_Gain4.Text = GetDNRRegister("CurStillPxWtGain4").ToString("X2");
            Cur_Still_Px_Wt_Gain5.Text = GetDNRRegister("CurStillPxWtGain5").ToString("X2");
            Cur_Still_Px_Wt_Gain6.Text = GetDNRRegister("CurStillPxWtGain6").ToString("X2");
            Cur_Still_Px_Wt_Gain7.Text = GetDNRRegister("CurStillPxWtGain7").ToString("X2");
            Cur_Still_Px_Wt_Gain8.Text = GetDNRRegister("CurStillPxWtGain8").ToString("X2");
 
            //这里计算需要放到文本框编辑
            SetMotionPixel();
            SetStillPixel();

        }
        /// <summary>
        /// 静止像素
        /// </summary>
        private void SetStillPixel() {
            Still_Dnr_Temp0.Text = PixelComputing(Cur_Still_Px_Wt_Gain0.Text, Still_Dnr_Adj_Gain0.Text).ToString("X2");
            Still_Dnr_Temp1.Text = PixelComputing(Cur_Still_Px_Wt_Gain1.Text, Still_Dnr_Adj_Gain1.Text).ToString("X2");
            Still_Dnr_Temp2.Text = PixelComputing(Cur_Still_Px_Wt_Gain2.Text, Still_Dnr_Adj_Gain2.Text).ToString("X2");
            Still_Dnr_Temp3.Text = PixelComputing(Cur_Still_Px_Wt_Gain3.Text, Still_Dnr_Adj_Gain3.Text).ToString("X2");
            Still_Dnr_Temp4.Text = PixelComputing(Cur_Still_Px_Wt_Gain4.Text, Still_Dnr_Adj_Gain4.Text).ToString("X2");
            Still_Dnr_Temp5.Text = PixelComputing(Cur_Still_Px_Wt_Gain5.Text, Still_Dnr_Adj_Gain5.Text).ToString("X2");
            Still_Dnr_Temp6.Text = PixelComputing(Cur_Still_Px_Wt_Gain6.Text, Still_Dnr_Adj_Gain6.Text).ToString("X2");
            Still_Dnr_Temp7.Text = PixelComputing(Cur_Still_Px_Wt_Gain7.Text, Still_Dnr_Adj_Gain7.Text).ToString("X2");
            Still_Dnr_Temp8.Text = PixelComputing(Cur_Still_Px_Wt_Gain8.Text, Still_Dnr_Adj_Gain8.Text).ToString("X2");
        }
        /// <summary>
        /// 运动像素
        /// </summary>
        private void SetMotionPixel() {
            Move_Dnr_Temp0.Text = PixelComputing(Cur_Move_Px_Wt_Gain0.Text, Move_Dnr_Adj_Gain0.Text).ToString("X2");
            Move_Dnr_Temp1.Text = PixelComputing(Cur_Move_Px_Wt_Gain1.Text, Move_Dnr_Adj_Gain1.Text).ToString("X2");
            Move_Dnr_Temp2.Text = PixelComputing(Cur_Move_Px_Wt_Gain2.Text, Move_Dnr_Adj_Gain2.Text).ToString("X2");
            Move_Dnr_Temp3.Text = PixelComputing(Cur_Move_Px_Wt_Gain3.Text, Move_Dnr_Adj_Gain3.Text).ToString("X2");
            Move_Dnr_Temp4.Text = PixelComputing(Cur_Move_Px_Wt_Gain4.Text, Move_Dnr_Adj_Gain4.Text).ToString("X2");
            Move_Dnr_Temp5.Text = PixelComputing(Cur_Move_Px_Wt_Gain5.Text, Move_Dnr_Adj_Gain5.Text).ToString("X2");
            Move_Dnr_Temp6.Text = PixelComputing(Cur_Move_Px_Wt_Gain6.Text, Move_Dnr_Adj_Gain6.Text).ToString("X2");
            Move_Dnr_Temp7.Text = PixelComputing(Cur_Move_Px_Wt_Gain7.Text, Move_Dnr_Adj_Gain7.Text).ToString("X2");
            Move_Dnr_Temp8.Text = PixelComputing(Cur_Move_Px_Wt_Gain8.Text, Move_Dnr_Adj_Gain8.Text).ToString("X2");
        }

        private long PixelComputing(string curpxwt, string dnsadj) {
            if (string.IsNullOrEmpty(curpxwt)||string.IsNullOrEmpty(dnsadj))
            {
                return 0;
            }

            long c1 = Convert.ToInt32(curpxwt, 16);
            long d1 = Convert.ToInt32(dnsadj, 16);

            long temp1 = (128 - c1);
            long temp2 = d1 < 64 ? d1 : (d1 - 128);
            long temp3 = temp1 / 4 + temp2;
            if (temp3 < 0)
            {
                return 0;

            }
            //else if (temp3 > 32)
            //{
            //    return 32;
            //}
            else
            {
                return temp3;
            }
        }

        private void PxWtManEnShowHide(bool isBool) {
            Wt_Pedestal.Enabled = isBool;
            Max_Move_Px_Wt.Enabled = isBool;
            Move_Diff_Ratio.Enabled = isBool;
            Cur_Move_Px_Wt.Enabled = !isBool;
            Cur_Still_Px_Wt.Enabled = !isBool;
        }
        private void MovePxWtCalEnShowHide(bool isBool) {
            Cur_Move_Px_Wt.Enabled = !isBool;
            Cur_Move_Px_Wt_Gain0.Enabled = isBool;
            Cur_Move_Px_Wt_Gain1.Enabled = isBool;
            Cur_Move_Px_Wt_Gain2.Enabled = isBool;
            Cur_Move_Px_Wt_Gain3.Enabled = isBool;
            Cur_Move_Px_Wt_Gain4.Enabled = isBool;
            Cur_Move_Px_Wt_Gain5.Enabled = isBool;
            Cur_Move_Px_Wt_Gain6.Enabled = isBool;
            Cur_Move_Px_Wt_Gain7.Enabled = isBool;
            Cur_Move_Px_Wt_Gain8.Enabled = isBool;

            if (Parn_en.CheckState != CheckState.Checked && isBool == true)
            {

            }
            else
            {
                labelControl33.Visible = isBool;
                Move_Dnr_Temp0.Visible = isBool;
                Move_Dnr_Temp1.Visible = isBool;
                Move_Dnr_Temp2.Visible = isBool;
                Move_Dnr_Temp3.Visible = isBool;
                Move_Dnr_Temp4.Visible = isBool;
                Move_Dnr_Temp5.Visible = isBool;
                Move_Dnr_Temp6.Visible = isBool;
                Move_Dnr_Temp7.Visible = isBool;
                Move_Dnr_Temp8.Visible = isBool;
            }

               

        }

        private void StillPxWtCalEnShowHide(bool isBool) {
            Cur_Still_Px_Wt.Enabled = !isBool;
            Cur_Still_Px_Wt_Gain0.Enabled = isBool;
            Cur_Still_Px_Wt_Gain1.Enabled = isBool;
            Cur_Still_Px_Wt_Gain2.Enabled = isBool;
            Cur_Still_Px_Wt_Gain3.Enabled = isBool;
            Cur_Still_Px_Wt_Gain4.Enabled = isBool;
            Cur_Still_Px_Wt_Gain5.Enabled = isBool;
            Cur_Still_Px_Wt_Gain6.Enabled = isBool;
            Cur_Still_Px_Wt_Gain7.Enabled = isBool;
            Cur_Still_Px_Wt_Gain8.Enabled = isBool;
            if (Parn_en.CheckState != CheckState.Checked && isBool == true)
            {

            }
            else 
            {
                labelControl17.Visible = isBool;
                Still_Dnr_Temp0.Visible = isBool;
                Still_Dnr_Temp1.Visible = isBool;
                Still_Dnr_Temp2.Visible = isBool;
                Still_Dnr_Temp3.Visible = isBool;
                Still_Dnr_Temp4.Visible = isBool;
                Still_Dnr_Temp5.Visible = isBool;
                Still_Dnr_Temp6.Visible = isBool;
                Still_Dnr_Temp7.Visible = isBool;
                Still_Dnr_Temp8.Visible = isBool;
            }
            
        }

        private void ThreeDParnEnShowHide(bool isBool) {
            Move_Dnr_Adj_Gain0.Enabled = isBool;
            Move_Dnr_Adj_Gain1.Enabled = isBool;
            Move_Dnr_Adj_Gain2.Enabled = isBool;
            Move_Dnr_Adj_Gain3.Enabled = isBool;
            Move_Dnr_Adj_Gain4.Enabled = isBool;
            Move_Dnr_Adj_Gain5.Enabled = isBool;
            Move_Dnr_Adj_Gain6.Enabled = isBool;
            Move_Dnr_Adj_Gain7.Enabled = isBool;
            Move_Dnr_Adj_Gain8.Enabled = isBool;
            Still_Dnr_Adj_Gain0.Enabled = isBool;
            Still_Dnr_Adj_Gain1.Enabled = isBool;
            Still_Dnr_Adj_Gain2.Enabled = isBool;
            Still_Dnr_Adj_Gain3.Enabled = isBool;
            Still_Dnr_Adj_Gain4.Enabled = isBool;
            Still_Dnr_Adj_Gain5.Enabled = isBool;
            Still_Dnr_Adj_Gain6.Enabled = isBool;
            Still_Dnr_Adj_Gain7.Enabled = isBool;
            Still_Dnr_Adj_Gain8.Enabled = isBool;

            if (isBool && Move_Px_Wt_Cal_en.CheckState != CheckState.Checked)
            {
                
            }
            else
            {
                labelControl33.Visible = isBool;
                Move_Dnr_Temp0.Visible = isBool;
                Move_Dnr_Temp1.Visible = isBool;
                Move_Dnr_Temp2.Visible = isBool;
                Move_Dnr_Temp3.Visible = isBool;
                Move_Dnr_Temp4.Visible = isBool;
                Move_Dnr_Temp5.Visible = isBool;
                Move_Dnr_Temp6.Visible = isBool;
                Move_Dnr_Temp7.Visible = isBool;
                Move_Dnr_Temp8.Visible = isBool;
            }

            if (isBool && Still_Px_Wt_Cal_en.CheckState != CheckState.Checked)
            {

            }
            else
            {
                labelControl17.Visible = isBool;
                Still_Dnr_Temp0.Visible = isBool;
                Still_Dnr_Temp1.Visible = isBool;
                Still_Dnr_Temp2.Visible = isBool;
                Still_Dnr_Temp3.Visible = isBool;
                Still_Dnr_Temp4.Visible = isBool;
                Still_Dnr_Temp5.Visible = isBool;
                Still_Dnr_Temp6.Visible = isBool;
                Still_Dnr_Temp7.Visible = isBool;
                Still_Dnr_Temp8.Visible = isBool;
            }
           

        }

        private void RawGslAndRawRbslShowHide(bool isBool)
        {
            Raw_Gsl0.Enabled = isBool;
            Raw_Gsl1.Enabled = isBool;
            Raw_Gsl2.Enabled = isBool;
            Raw_Gsl3.Enabled = isBool;
            Raw_Gsl4.Enabled = isBool;
            Raw_Gsl5.Enabled = isBool;
            Raw_Gsl6.Enabled = isBool;
            Raw_Gsl7.Enabled = isBool;
            Raw_Gsl8.Enabled = isBool;
            Raw_Gsl9.Enabled = isBool;
            Raw_Rbsl0.Enabled = isBool;
            Raw_Rbsl1.Enabled = isBool;
            Raw_Rbsl2.Enabled = isBool;
            Raw_Rbsl3.Enabled = isBool;
            Raw_Rbsl4.Enabled = isBool;
            Raw_Rbsl5.Enabled = isBool;
            Raw_Rbsl6.Enabled = isBool;
            Raw_Rbsl7.Enabled = isBool;
            Raw_Rbsl8.Enabled = isBool;
            Raw_Rbsl9.Enabled = isBool;
        }
        private void UvDnsManualShowHide(bool isBool) {
            Uv_Dnr0.Enabled = isBool;
            Uv_Dnr1.Enabled = isBool;
            Uv_Dnr2.Enabled = isBool;
            Uv_Dnr3.Enabled = isBool;
            Uv_Dnr4.Enabled = isBool;
            Uv_Dnr5.Enabled = isBool;
            Uv_Dnr6.Enabled = isBool;
            Uv_Dnr7.Enabled = isBool;
            Uv_Dnr8.Enabled = isBool;
            Uv_Dnr9.Enabled = isBool;
            Uv_DNR_Manual.Enabled = !isBool;
        }
        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XChip_DNS_Load(object sender, EventArgs e)
        {
            //if (ConfigurationManager.AppSettings["xcEdition"] == "1") {
            //    simpleButton1.Text = "获取参数";
            //    simpleButton6.Text = "调试指南";
            //}
            //group3ddnr.Hide();
            Task.Run(() => {
            this.Invoke((EventHandler)delegate {
                for (int i = 0; i < XChipDnr.Count; i++)
                {

                    var type = AppHelper.NameTypeToString(XChipDnr[i].NameType);
                    //加上单选框的_en
                    if (XChipDnr[i].ControlType == "1")
                    {
                        type = XChipDnr[i].NameType;
                    }
                    try
                    {
                        var col = this.Controls.Find(type, true);


                        foreach (var item in col)
                        {
                            if (item is TextEdit)
                            {
                                //声明一个SuerToolTip
                                DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                                //声明一个ToolTipTitleItem,保存标题
                                DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                                String tooltips = "";

                                if (!string.IsNullOrEmpty(XChipDnr[i].Bit_Wide))
                                {
                                    tooltips += $"位宽={XChipDnr[i].Bit_Wide}";
                                }
                                if (!string.IsNullOrEmpty(XChipDnr[i].Bit_Accuracy))
                                {
                                    tooltips += $"|精度={XChipDnr[i].Bit_Accuracy}";
                                }
                                //设置标题文字
                                toolTipTitleItem.Text = tooltips;
                                //把标题添加到SuerToolTip
                                graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                                //指定控件的SuperTip
                                ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                                //正则判断
                                ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                                ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipDnr[i].Bits, XChipDnr[i].Bit_Wide, XChipDnr.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());
                            }
                            }


                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
                for (int i = 0; i < XChipSnr.Count; i++)
                {

                    var type = AppHelper.NameTypeToString(XChipSnr[i].NameType);
                    var col = this.Controls.Find(type, true);


                    foreach (var item in col)
                    {
                        if (item is TextEdit)
                        {
                            //声明一个SuerToolTip
                            DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                            //声明一个ToolTipTitleItem,保存标题
                            DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                            String tooltips = "";

                            if (!string.IsNullOrEmpty(XChipSnr[i].Bit_Wide))
                            {
                                tooltips += $"位宽={XChipSnr[i].Bit_Wide}";
                            }
                            if (!string.IsNullOrEmpty(XChipSnr[i].Bit_Accuracy))
                            {
                                tooltips += $"|精度={XChipSnr[i].Bit_Accuracy}";
                            }
                            //设置标题文字
                            toolTipTitleItem.Text = tooltips;
                            //把标题添加到SuerToolTip
                            graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                            //指定控件的SuperTip
                            ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                            //正则判断
                            ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                            ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipSnr[i].Bits, XChipSnr[i].Bit_Wide, XChipSnr.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());
                        }
                    }
                }
            });
            });
        }
        #endregion
    
        private void SetDnsRegister(addressregister model, ulong add, bool isCheck = true)
        {
            //if (!sPortTemp.IsOpen)//串口检测
            //{
            //    DevExpress.XtraEditors.XtraMessageBox.Show("串口未打开");
            //    return;
            //}
            //if (isRead) return;
            //if (XChipDnr.Where(c => c.NameType == nameType).Count() > 0)
            //{
            //    SerialSend.WriteRegister(Convert.ToUInt32(XChipDnr.Where(c => c.NameType == nameType).First().Address, 16), add, sPortTemp);
            //}
            if (isRead) return;
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)//串口检测
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("串口未打开");
                return;
            }
           
            if (IsUsbOpen)
            {
                if (null != BinHelp.usbList)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));


                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }

                if (null != BinHelp.lspList)
                { //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), (uint)temp);
                }
            }


            SerialSend.WriteRegister(Convert.ToUInt32(model.Address, 16), add, sPortTemp);
            
        }
        #region 勾选框changed
        private void PxWtManEn_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var txt = sender as CheckEdit;
            nr3d_wt_en.Enabled = !txt.Checked;
            if (nr3d_wt_en.Enabled)
            {
                nr3d_wtEnShowHide(nr3d_wt_en.Checked);
            }
            else
            {
                nr3d_wtEnShowHide(false);
            }
            //var nValue = SerialSend.ReadRegister(Convert.ToUInt32(XChipDnr.Where(c => c.NameType == "PxWtManEn").First().Address, 16), sPortTemp);
            //var nValue = GetDNRRegister("PxWtManEn");
            if (Px_Wt_Man_en.CheckState == CheckState.Checked) //这个是勾选下发值
            {
                //SetDnsRegister("PxWtManEn", (ulong)(nValue | 0x0c));
                if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                    var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                    var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Px_Wt_Man_en.Checked);
                    SetDnsRegister(model, Val);
                }
                if (XChipDnr.Exists(c => c.NameType.ToLower() == ("Px_Wt_Man3_en").ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == ("Px_Wt_Man3_en").ToLower());
                    var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                    var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Px_Wt_Man_en.Checked);
                    SetDnsRegister(model, Val);
                }
                PxWtManEnShowHide(false);
                Move_Px_Wt_Cal_en.Enabled = true;
                Still_Px_Wt_Cal_en.Enabled = true;

                MovePxWtCalEnShowHide(Move_Px_Wt_Cal_en.Checked);
                StillPxWtCalEnShowHide(Still_Px_Wt_Cal_en.Checked);

                if (Move_Px_Wt_Cal_en.CheckState == CheckState.Checked|| Move_Px_Wt_Cal_en.CheckState == CheckState.Checked)
                {
                    var Adden = SerialSend.ReadRegister(Convert.ToUInt32("200014de", 16), sPortTemp);
                    var Valen = BitsHelp.SetBitsCheckedToUint("[0]", "1", Adden.ToString("X2"), true);
                    SerialSend.WriteRegister(0x200014de, Valen, sPortTemp);
                }
            }
            else
            {

                //SetDnsRegister("PxWtManEn", (ulong)(nValue & 0xf3));
                if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                    var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                    var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Px_Wt_Man_en.Checked);
                    SetDnsRegister(model, Val);
                }
                if (XChipDnr.Exists(c => c.NameType.ToLower() == ("Px_Wt_Man3_en").ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == ("Px_Wt_Man3_en").ToLower());
                    var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                    var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Px_Wt_Man_en.Checked);
                    SetDnsRegister(model, Val);
                }
                PxWtManEnShowHide(true);
                Move_Px_Wt_Cal_en.Enabled = false;
                Still_Px_Wt_Cal_en.Enabled = false;
                MovePxWtCalEnShowHide(false);
                StillPxWtCalEnShowHide(false);
                Cur_Move_Px_Wt.Enabled = false;
                Cur_Still_Px_Wt.Enabled = false;


                var  Adden = SerialSend.ReadRegister(Convert.ToUInt32("200014de", 16), sPortTemp);
                var  Valen = BitsHelp.SetBitsCheckedToUint("[0]", "1", Adden.ToString("X2"), false);
                SerialSend.WriteRegister(0x200014de, Valen, sPortTemp);

            }
            
        }

        private void MovePxWtCalEn_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //if (Move_Px_Wt_Cal_En.CheckState == CheckState.Checked) //这个是勾选下发值
            //{
            //    SetDnsRegister("MovePxWtCalEn", 0x01);
            //    if (Px_Wt_Man_En.CheckState == CheckState.Checked)
            //    {
            //        MovePxWtCalEnShowHide(true);
            //        //CurMovePxWt.Enabled = false;
            //    }
            //    else
            //    {
            //        MovePxWtCalEnShowHide(false);
            //        //CurMovePxWt.Enabled = true;
            //    }
            //}
            //else
            //{
            //    SetDnsRegister("MovePxWtCalEn", 0x00);
            //    MovePxWtCalEnShowHide(false);
            //}
            if (isRead) return;
            var txt = sender as CheckEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Move_Px_Wt_Cal_en.Checked);
                SetDnsRegister(model, Val);
            }
            if (Move_Px_Wt_Cal_en.CheckState == CheckState.Checked)
            {
                if (Px_Wt_Man_en.CheckState == CheckState.Checked)
                {
                    MovePxWtCalEnShowHide(true);
                    //CurStillPxWt.Enabled = false;
                }
                else
                {
                    MovePxWtCalEnShowHide(false);
                    //CurStillPxWt.Enabled = true;
                }
                var Adden = SerialSend.ReadRegister(Convert.ToUInt32("200014de", 16), sPortTemp);
                var Valen = BitsHelp.SetBitsCheckedToUint("[0]", "1", Adden.ToString("X2"), true);
                SerialSend.WriteRegister(0x200014de, Valen, sPortTemp);
            }
            else
            {
                MovePxWtCalEnShowHide(false);
                if (Move_Px_Wt_Cal_en.CheckState != CheckState.Checked)
                {
                    var Adden = SerialSend.ReadRegister(Convert.ToUInt32("200014de", 16), sPortTemp);
                    var Valen = BitsHelp.SetBitsCheckedToUint("[0]", "1", Adden.ToString("X2"), false);
                    SerialSend.WriteRegister(0x200014de, Valen, sPortTemp);
                }
            }
        }

        private void StillPxWtCalEn_CheckedChanged(object sender, EventArgs e)
        {
            //if (isRead) return;
            //if (Still_Px_Wt_Cal_En.CheckState == CheckState.Checked) //这个是勾选下发值
            //{
            //    SetDnsRegister("StillPxWtCalEn", 0x01);
            //    if (Px_Wt_Man_En.CheckState == CheckState.Checked)
            //    {
            //        StillPxWtCalEnShowHide(true);
            //        //CurStillPxWt.Enabled = false;
            //    }
            //    else
            //    {
            //        StillPxWtCalEnShowHide(false);
            //        //CurStillPxWt.Enabled = true;
            //    }
            //}
            //else
            //{
            //    SetDnsRegister("StillPxWtCalEn", 0x00);
            //    StillPxWtCalEnShowHide(false);
            //}
            if (isRead) return;
            var txt = sender as CheckEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetDnsRegister(model, Val);
            }
            if (Still_Px_Wt_Cal_en.CheckState == CheckState.Checked)
            {
                if (Px_Wt_Man_en.CheckState == CheckState.Checked)
                {
                    StillPxWtCalEnShowHide(true);
                    //CurStillPxWt.Enabled = false;
                }
                else
                {
                    StillPxWtCalEnShowHide(false);
                    //CurStillPxWt.Enabled = true;
                }
                var Adden = SerialSend.ReadRegister(Convert.ToUInt32("200014de", 16), sPortTemp);
                var Valen = BitsHelp.SetBitsCheckedToUint("[0]", "1", Adden.ToString("X2"), true);
                SerialSend.WriteRegister(0x200014de, Valen, sPortTemp);

            }
            else
            {
                StillPxWtCalEnShowHide(false);
                if (Move_Px_Wt_Cal_en.CheckState != CheckState.Checked)
                {
                    var Adden = SerialSend.ReadRegister(Convert.ToUInt32("200014de", 16), sPortTemp);
                    var Valen = BitsHelp.SetBitsCheckedToUint("[0]", "1", Adden.ToString("X2"), false);
                    SerialSend.WriteRegister(0x200014de, Valen, sPortTemp);
                }
            }
        }

        private void ParnEn_CheckedChanged(object sender, EventArgs e)
        {

            //var nValue = GetDNRRegister("ParnEn");
            //if (Parn_En.CheckState == CheckState.Checked) //这个是勾选下发值
            //{
            //    SetDnsRegister("ParnEn", (ulong)(nValue | 0x01));
            //    ThreeDParnEnShowHide(true);
            //    RawGslAndRawRbslShowHide(false);
            //}
            //else
            //{
            //    SetDnsRegister("ParnEn", (ulong)(nValue & 0xfe));
            //    ThreeDParnEnShowHide(false);
            //    RawGslAndRawRbslShowHide(true);
            //}
            if (isRead) return;
            var txt = sender as CheckEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                //bit1置0才是勾选所以要反一下
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), !Parn_en.Checked);
                SetDnsRegister(model, Val);
            }
            if (Parn_en.CheckState == CheckState.Checked)
            {
                ThreeDParnEnShowHide(true);
                RawGslAndRawRbslShowHide(false);
            }
            else
            {
                ThreeDParnEnShowHide(false);
                RawGslAndRawRbslShowHide(true);
            }
        }

        private void UvDNSManualEn_CheckedChanged(object sender, EventArgs e)
        {

            //var nValue = GetDNRRegister("UvDNSManualEn");
            //if (Uv_DNR_Manual_En.CheckState == CheckState.Checked) //这个是勾选下发值
            //{
            //    SetDnsRegister("UvDNSManualEn", (ulong)(nValue | 0x80));
            //    Uv_DNR_Manual_En.CheckState = CheckState.Checked;
            //    UvDnsManualShowHide(false);
            //}
            //else
            //{
            //    SetDnsRegister("UvDNSManualEn", (ulong)(nValue & 0x7f));
            //    Uv_DNR_Manual_En.CheckState = CheckState.Unchecked;
            //    UvDnsManualShowHide(true);
            //}
            if (isRead) return;
            var txt = sender as DevExpress.XtraEditors.TextEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Uv_DNR_Manual_En.Checked);
                SetDnsRegister(model, Val);
            }
            if (Uv_DNR_Manual_En.CheckState == CheckState.Checked)
            {
                Uv_DNR_Manual_En.CheckState = CheckState.Checked;
                UvDnsManualShowHide(false);
            }
            else
            {
                Uv_DNR_Manual_En.CheckState = CheckState.Unchecked;
                UvDnsManualShowHide(true);
            }
        }
        #endregion

        private void RawSigma0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0": SetDnsRegister("RawSigma0", (ulong)Convert.ToInt32(Raw_Sigma0.Text, 16)); break;
            //    case "1": SetDnsRegister("RawSigma1", (ulong)Convert.ToInt32(Raw_Sigma1.Text, 16)); break;
            //    case "2": SetDnsRegister("RawSigma2", (ulong)Convert.ToInt32(Raw_Sigma2.Text, 16)); break;
            //    case "3": SetDnsRegister("RawSigma3", (ulong)Convert.ToInt32(Raw_Sigma3.Text, 16)); break;
            //    case "4": SetDnsRegister("RawSigma4", (ulong)Convert.ToInt32(Raw_Sigma4.Text, 16)); break;
            //    case "5": SetDnsRegister("RawSigma5", (ulong)Convert.ToInt32(Raw_Sigma5.Text, 16)); break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void RawGsl0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0": SetDnsRegister("RawGsl0", (ulong)Convert.ToInt32(Raw_Gsl0.Text, 16)); break;
            //    case "1": SetDnsRegister("RawGsl1", (ulong)Convert.ToInt32(Raw_Gsl1.Text, 16)); break;
            //    case "2": SetDnsRegister("RawGsl2", (ulong)Convert.ToInt32(Raw_Gsl2.Text, 16)); break;
            //    case "3": SetDnsRegister("RawGsl3", (ulong)Convert.ToInt32(Raw_Gsl3.Text, 16)); break;
            //    case "4": SetDnsRegister("RawGsl4", (ulong)Convert.ToInt32(Raw_Gsl4.Text, 16)); break;
            //    case "5": SetDnsRegister("RawGsl5", (ulong)Convert.ToInt32(Raw_Gsl5.Text, 16)); break;
            //    case "6": SetDnsRegister("RawGsl6", (ulong)Convert.ToInt32(Raw_Gsl6.Text, 16)); break;
            //    case "7": SetDnsRegister("RawGsl7", (ulong)Convert.ToInt32(Raw_Gsl7.Text, 16)); break;
            //    case "8":
            //        SetDnsRegister("RawGsl8", (ulong)Convert.ToInt32(Raw_Gsl8.Text, 16));
            //        SetDnsRegister("RawGsl9", (ulong)Convert.ToInt32(Raw_Gsl8.Text, 16));
            //        break;
            //    case "9": SetDnsRegister("RawGsl9", (ulong)Convert.ToInt32(Raw_Gsl9.Text, 16)); break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void RawRbsl0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0": SetDnsRegister("RawRbsl0", (ulong)Convert.ToInt32(Raw_Rbsl0.Text, 16)); break;
            //    case "1": SetDnsRegister("RawRbsl1", (ulong)Convert.ToInt32(Raw_Rbsl1.Text, 16)); break;
            //    case "2": SetDnsRegister("RawRbsl2", (ulong)Convert.ToInt32(Raw_Rbsl2.Text, 16)); break;
            //    case "3": SetDnsRegister("RawRbsl3", (ulong)Convert.ToInt32(Raw_Rbsl3.Text, 16)); break;
            //    case "4": SetDnsRegister("RawRbsl4", (ulong)Convert.ToInt32(Raw_Rbsl4.Text, 16)); break;
            //    case "5": SetDnsRegister("RawRbsl5", (ulong)Convert.ToInt32(Raw_Rbsl5.Text, 16)); break;
            //    case "6": SetDnsRegister("RawRbsl6", (ulong)Convert.ToInt32(Raw_Rbsl6.Text, 16)); break;
            //    case "7": SetDnsRegister("RawRbsl7", (ulong)Convert.ToInt32(Raw_Rbsl7.Text, 16)); break;
            //    case "8":
            //        SetDnsRegister("RawRbsl8", (ulong)Convert.ToInt32(Raw_Rbsl8.Text, 16));
            //        SetDnsRegister("RawRbsl9", (ulong)Convert.ToInt32(Raw_Rbsl8.Text, 16)); break;
            //    case "9": SetDnsRegister("RawRbsl9", (ulong)Convert.ToInt32(Raw_Rbsl9.Text, 16)); break;
            //    default:
            //        break;
            //}
        }

        private void YuvSigma0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0": SetDnsRegister("YuvSigma0", (ulong)Convert.ToInt32(Yuv_Sigma0.Text, 16)); break;
            //    case "1": SetDnsRegister("YuvSigma1", (ulong)Convert.ToInt32(Yuv_Sigma1.Text, 16)); break;
            //    case "2": SetDnsRegister("YuvSigma2", (ulong)Convert.ToInt32(Yuv_Sigma2.Text, 16)); break;
            //    case "3": SetDnsRegister("YuvSigma3", (ulong)Convert.ToInt32(Yuv_Sigma3.Text, 16)); break;
            //    case "4": SetDnsRegister("YuvSigma4", (ulong)Convert.ToInt32(Yuv_Sigma4.Text, 16)); break;
            //    case "5": SetDnsRegister("YuvSigma5", (ulong)Convert.ToInt32(Yuv_Sigma5.Text, 16)); break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void Ysl0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0": SetDnsRegister("Ysl0", (ulong)Convert.ToInt32(Ysl0.Text, 16)); break;
            //    case "1": SetDnsRegister("Ysl1", (ulong)Convert.ToInt32(Ysl1.Text, 16)); break;
            //    case "2": SetDnsRegister("Ysl2", (ulong)Convert.ToInt32(Ysl2.Text, 16)); break;
            //    case "3": SetDnsRegister("Ysl3", (ulong)Convert.ToInt32(Ysl3.Text, 16)); break;
            //    case "4": SetDnsRegister("Ysl4", (ulong)Convert.ToInt32(Ysl4.Text, 16)); break;
            //    case "5": SetDnsRegister("Ysl5", (ulong)Convert.ToInt32(Ysl5.Text, 16)); break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void UvDns0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0": SetDnsRegister("UvDns0", (ulong)Convert.ToInt32(Uv_Dnr0.Text, 16)); break;
            //    case "1": SetDnsRegister("UvDns1", (ulong)Convert.ToInt32(Uv_Dnr1.Text, 16)); break;
            //    case "2": SetDnsRegister("UvDns2", (ulong)Convert.ToInt32(Uv_Dnr2.Text, 16)); break;
            //    case "3": SetDnsRegister("UvDns3", (ulong)Convert.ToInt32(Uv_Dnr3.Text, 16)); break;
            //    case "4": SetDnsRegister("UvDns4", (ulong)Convert.ToInt32(Uv_Dnr4.Text, 16)); break;
            //    case "5": SetDnsRegister("UvDns5", (ulong)Convert.ToInt32(Uv_Dnr5.Text, 16)); break;
            //    case "6": SetDnsRegister("UvDns6", (ulong)Convert.ToInt32(Uv_Dnr6.Text, 16)); break;
            //    case "7": SetDnsRegister("UvDns7", (ulong)Convert.ToInt32(Uv_Dnr7.Text, 16)); break;
            //    case "8":
            //        SetDnsRegister("UvDns8", (ulong)Convert.ToInt32(Uv_Dnr8.Text, 16));
            //        SetDnsRegister("UvDns9", (ulong)Convert.ToInt32(Uv_Dnr8.Text, 16));
            //        break;
            //    case "9": SetDnsRegister("UvDns9", (ulong)Convert.ToInt32(Uv_Dnr9.Text, 16)); break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void UvDnsManual_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //SetDnsRegister("UvDnsManual", (ulong)Convert.ToInt32(Uv_DNR_Manual.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Uv_DNR_Manual.Text)) return;
            var txt = sender as DevExpress.XtraEditors.TextEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Uv_DNR_Manual.Text);
                SetDnsRegister(model, Val);
            }
        }

        private void WtPedestal_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
                //case "0":
                //    {
                //        SetDnsRegister("WtPedestal", (ulong)Convert.ToInt32(txtEdit.Text, 16));
                //    }
                //    break;
                //case "1":
                //    {
                //        SetDnsRegister("MaxMovePxWt", (ulong)Convert.ToInt32(txtEdit.Text, 16));
                //    }
                //    break;
                //case "2":
                //    {
                //        SetDnsRegister("MoveDiffRatio", (ulong)Convert.ToInt32(txtEdit.Text, 16));
                //    }
                //    break;
                //default:
                //    break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void CurMovePxWt_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //SetDnsRegister("CurMovePxWt", (ulong)Convert.ToInt32(Cur_Move_Px_Wt.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Cur_Move_Px_Wt.Text)) return;
            var txt = sender as DevExpress.XtraEditors.TextEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Cur_Move_Px_Wt.Text);
                SetDnsRegister(model, Val);
            }

        }

        private void CurStillPxWt_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //SetDnsRegister("CurStillPxWt", (ulong)Convert.ToInt32(Cur_Still_Px_Wt.Text, 16));
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(Cur_Still_Px_Wt.Text)) return;
            var txt = sender as DevExpress.XtraEditors.TextEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Cur_Still_Px_Wt.Text);
                SetDnsRegister(model, Val);
            }
        }

        private void Slope0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //var str = Math.Pow(2, Convert.ToInt16(txtEdit.Tag)).ToString();
            //SetDNRRegister($"thr_slope_{str}x", Convert.ToUInt16(txtEdit.Text, 16));
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void Offset0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //var str = Math.Pow(2, Convert.ToInt16(txtEdit.Tag)).ToString();
            //SetDNRRegister($"thr_offset_{str}x", Convert.ToUInt16(txtEdit.Text, 16));
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void CurMovePxWtGain0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0":
            //        {
            //            SetDnsRegister("CurMovePxWtGain0", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "1":
            //        {
            //            SetDnsRegister("CurMovePxWtGain1", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "2":
            //        {
            //            SetDnsRegister("CurMovePxWtGain2", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "3":
            //        {
            //            SetDnsRegister("CurMovePxWtGain3", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "4":
            //        {
            //            SetDnsRegister("CurMovePxWtGain4", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "5":
            //        {
            //            SetDnsRegister("CurMovePxWtGain5", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "6":
            //        {
            //            SetDnsRegister("CurMovePxWtGain6", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "7":
            //        {
            //            SetDnsRegister("CurMovePxWtGain7", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "8":
            //        {
            //            SetDnsRegister("CurMovePxWtGain8", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
            SetMotionPixel();
            SetStillPixel();
        }

        private void CurStillPxWtGain0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //switch (txtEdit.Tag.ToString())
            //{
            //    case "0":
            //        {
            //            SetDnsRegister("CurStillPxWtGain0", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "1":
            //        {
            //            SetDnsRegister("CurStillPxWtGain1", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "2":
            //        {
            //            SetDnsRegister("CurStillPxWtGain2", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "3":
            //        {
            //            SetDnsRegister("CurStillPxWtGain3", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "4":
            //        {
            //            SetDnsRegister("CurStillPxWtGain4", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "5":
            //        {
            //            SetDnsRegister("CurStillPxWtGain5", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "6":
            //        {
            //            SetDnsRegister("CurStillPxWtGain6", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "7":
            //        {
            //            SetDnsRegister("CurStillPxWtGain7", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    case "8":
            //        {
            //            SetDnsRegister("CurStillPxWtGain8", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //        }
            //        break;
            //    default:
            //        break;
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
            SetMotionPixel();
            SetStillPixel();
        }

        private void MoveDnsAdjGain0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //var nValue = Convert.ToUInt32(txtEdit.Text, 16);
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                var nValue = Convert.ToUInt32(txt.Text, 16);
                if (nValue > 0x7f)
                {
                    if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, "7F");
                        SetDnsRegister(model, Val);
                    }
                    txt.Text = "7F";
                }
                else
                {
                    if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDnsRegister(model, Val);
                    }
                }
                
            }
            //if (nValue > 0x7f)
            //{

            //    switch (txtEdit.Tag.ToString())
            //    {
            //        case "0":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain0", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain0.Text = "7F";
            //            }
            //            break;
            //        case "1":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain1", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain1.Text = "7F";
            //            }
            //            break;
            //        case "2":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain2", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain2.Text = "7F";
            //            }
            //            break;
            //        case "3":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain3", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain3.Text = "7F";
            //            }
            //            break;
            //        case "4":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain4", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain4.Text = "7F";
            //            }
            //            break;
            //        case "5":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain5", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain5.Text = "7F";
            //            }
            //            break;
            //        case "6":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain6", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain6.Text = "7F";
            //            }
            //            break;
            //        case "7":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain7", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain7.Text = "7F";
            //            }
            //            break;
            //        case "8":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain8", (ulong)0x7f);
            //                Move_Dnr_Adj_Gain8.Text = "7F";
            //            }
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //else
            //{
            //    switch (txtEdit.Tag.ToString())
            //    {
            //        case "0":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain0", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "1":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain1", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "2":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain2", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "3":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain3", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "4":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain4", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "5":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain5", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "6":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain6", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "7":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain7", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "8":
            //            {
            //                SetDnsRegister("MoveDnsAdjGain8", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        default:
            //            break;
            //    }
            //}
            SetMotionPixel();
            SetStillPixel();
        }

        private void StillDnsAdjGain0_Leave(object sender, EventArgs e)
        {
            //if (isWrite) return;
            //var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
            //if (string.IsNullOrEmpty(txtEdit.Text)) return;
            //var nValue = Convert.ToUInt32(txtEdit.Text, 16);
            //if (nValue > 0x7f)
            //{
            //    switch (txtEdit.Tag.ToString())
            //    {
            //        case "0":
            //            {
            //                SetDnsRegister("StillDnsAdjGain0", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain0.Text = "7F";
            //            }
            //            break;
            //        case "1":
            //            {
            //                SetDnsRegister("StillDnsAdjGain1", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain1.Text = "7F";
            //            }
            //            break;
            //        case "2":
            //            {
            //                SetDnsRegister("StillDnsAdjGain2", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain2.Text = "7F";
            //            }
            //            break;
            //        case "3":
            //            {
            //                SetDnsRegister("StillDnsAdjGain3", (ulong)0x7f);
            //                StillDnsAdjGain3.Text = "7F";
            //            }
            //            break;
            //        case "4":
            //            {
            //                SetDnsRegister("StillDnsAdjGain4", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain4.Text = "7F";
            //            }
            //            break;
            //        case "5":
            //            {
            //                SetDnsRegister("StillDnsAdjGain5", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain5.Text = "7F";
            //            }
            //            break;
            //        case "6":
            //            {
            //                SetDnsRegister("StillDnsAdjGain6", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain6.Text = "7F";
            //            }
            //            break;
            //        case "7":
            //            {
            //                SetDnsRegister("StillDnsAdjGain7", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain7.Text = "7F";
            //            }
            //            break;
            //        case "8":
            //            {
            //                SetDnsRegister("StillDnsAdjGain8", (ulong)0x7f);
            //                Still_Dnr_Adj_Gain8.Text = "7F";
            //            }
            //            break;
            //        default:
            //            break;
            //    }
            //}
            //else
            //{
            //    switch (txtEdit.Tag.ToString())
            //    {
            //        case "0":
            //            {
            //                SetDnsRegister("StillDnsAdjGain0", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "1":
            //            {
            //                SetDnsRegister("StillDnsAdjGain1", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "2":
            //            {
            //                SetDnsRegister("StillDnsAdjGain2", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "3":
            //            {
            //                SetDnsRegister("StillDnsAdjGain3", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "4":
            //            {
            //                SetDnsRegister("StillDnsAdjGain4", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "5":
            //            {
            //                SetDnsRegister("StillDnsAdjGain5", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "6":
            //            {
            //                SetDnsRegister("StillDnsAdjGain6", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "7":
            //            {
            //                SetDnsRegister("StillDnsAdjGain7", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        case "8":
            //            {
            //                SetDnsRegister("StillDnsAdjGain8", (ulong)Convert.ToInt32(txtEdit.Text, 16));
            //            }
            //            break;
            //        default:
            //            break;
            //    }
            //}
            if (isWrite) return;
            if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                var nValue = Convert.ToUInt32(txt.Text, 16);
                if (nValue > 0x7f)
                {
                    if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, "7F");
                        SetDnsRegister(model, Val);
                    }
                    txt.Text = "7F";
                }
                else
                {
                    if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDnsRegister(model, Val);
                    }
                }

            }
            SetMotionPixel();
            SetStillPixel();
        }

        private void RawSigma0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RawSigma0_Leave(sender, null);
            }
        }

        private void RawGsl0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RawGsl0_Leave(sender, null);
            }
        }

        private void RawRbsl0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                RawRbsl0_Leave(sender, null);
            }
        }

        private void YuvSigma0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                YuvSigma0_Leave(sender, null);
            }
        }

        private void Ysl0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Ysl0_Leave(sender, null);
            }
        }

        private void UvDns0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UvDns0_Leave(sender, null);
            }
        }

        private void UvDnsManual_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UvDnsManual_Leave(sender, null);
            }
        }

        private void WtPedestal_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                WtPedestal_Leave(sender, null);
            }
        }

        private void CurMovePxWt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CurMovePxWt_Leave(sender, null);
            }
        }

        private void CurStillPxWt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CurStillPxWt_Leave(sender, null);
            }
        }

        private void Slope0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Slope0_Leave(sender, null);
            }
        }

        private void Offset0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Offset0_Leave(sender, null);
            }
        }

        private void CurMovePxWtGain0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CurMovePxWtGain0_Leave(sender, null);
            }
        }

        private void CurStillPxWtGain0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CurStillPxWtGain0_Leave(sender, null);
            }
        }

        private void MoveDnsAdjGain0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MoveDnsAdjGain0_Leave(sender, null);
            }
        }

        private void StillDnsAdjGain0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                StillDnsAdjGain0_Leave(sender, null);
            }
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            //string str = "       根据不同增益值，调节RAW域或YUV域的去噪等级；\r\n       其中 Raw_Sigma 值越大去噪越强；Gsl/Rbsl 值越大去噪越弱（取值范围为 0x00~0x20），分别决定 G 分量上和 RB 分量上的去噪程度，作用于RAW域中。Yuv_Sigma 值越大去噪越强；Ysl值越大去噪越弱（取值范围为 0x00~0x20），且主要决定 Y 分量上的去噪程度；Uv_dns 值越大，彩噪的去噪程度越强，作用于 YUV 域中。\r\n       部分型号isp支持时域去噪（3DNR）；其中“Px_Wt_Man_en”未勾选，则为3DNR 的强度自动调节模式，Wt_Pedestal 的取值范围为 0x00~0x80，值越大，3D 去噪越强，运动拖尾越严重；勾选后，则为3DNR 的强度手动调节模式，同Cur_Move_Px_Wt_Gain与Cur_Still_Px_Wt_Gain联调，设置不同增益下的 3DNR 强度，取值范围为 0x00~0x80，值越大，3D 去噪越弱。\r\n       更多详见《调试工具用户手册》。";
            Thread thread = new Thread(threadPro);
            thread.Start();
        }
        private void threadPro()

        {

            MethodInvoker methodInvoker = new MethodInvoker(ShowFormB);

            BeginInvoke(methodInvoker);

        }

        private void ShowFormB()
        {
            //string str = "\t      \t 根据不同增益值，调节RAW域或YUV域的去噪等级；\r\n"
            //   + "\t      \t 其中 Raw_Sigma 值越大去噪越强；Gsl/Rbsl 值越大去噪越弱（取值范围为 0x00~0x20），分别决定 G 分量上和 RB 分量上的去噪程度，作用于RAW域中。Yuv_Sigma 值越大去噪越强；Ysl值越大去噪越弱（取值范围为 0x00~0x20），且主要决定 Y 分量上的去噪程度；Uv_dns 值越大，彩噪的去噪程度越强，作用于 YUV 域中。\r\n"
            //   + "\t      \t 部分型号isp支持时域去噪（3DNR）；其中“Px_Wt_Man_en”未勾选，则为3DNR 的强度自动调节模式，Wt_Pedestal 的取值范围为 0x00~0x80，值越大，3D 去噪越强，运动拖尾越严重；勾选后，则为3DNR 的强度手动调节模式，同Cur_Move_Px_Wt_Gain与Cur_Still_Px_Wt_Gain联调，设置不同增益下的 3DNR 强度，取值范围为 0x00~0x80，值越大，3D 去噪越弱。\r\n"
            //   + "\t      \t 为减少和消除对于噪声的放大效应，Med_filter_scale用来调节滤波的强度，值越大，滤波越强，同时通过调节thr_slope/thr_offset控制噪声模型曲线，噪声曲线越陡峭时，对应的降噪效果越强。\r\n"
            //   + "\t      \t Lap_enhance_scale为增强倍率，其中 value =0x10 为 1 倍增强，value <0x10为小于 1 倍增强，value >0x10 为大于 1 倍增强。\r\n"
            //   + "\t      \t 更多详见《调试工具用户手册》。\r\n";
            string str = "";
            XChip_OptimizationTips ot = new XChip_OptimizationTips("DNS调优Tips", str);
            ot.StartPosition = FormStartPosition.CenterScreen;
            //ot.ShowDialog();
            ot.Show();

        }

        private void txt_Lap_enhance_scale_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //SetDNRRegister(Lap_enhance_scale.Tag.ToString(), Convert.ToUInt16(Lap_enhance_scale.Text, 16));
                if(e.KeyCode == Keys.Enter)
                {
                    //SetDNRRegister(Med_filter_sclae.Tag.ToString(), Convert.ToUInt16(Med_filter_sclae.Text, 16));
                    //if (isWrite) return;
                    //if (isRead) return; //判断是否在读，读的时候不进行写           
                    //if (string.IsNullOrEmpty(Uv_DNR_Manual.Text)) return;
                    //if (XChipDnr.Exists(c => c.NameType.ToLower() == ($"Lap_enhance_scale").ToLower()))
                    //{
                    //    var model = XChipDnr.Find(c => c.NameType.ToLower() == ($"Lap_enhance_scale").ToLower());
                    //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Lap_enhance_scale.Text);
                    //    SetDnsRegister(model, Val);
                    //}
                }
            }
        }

        private void txt_Lap_enhance_scale_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Lap_enhance_scale_KeyDown(sender, keyEnter);
        }

        private void txt_Med_filter_sclae_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //SetDNRRegister(Med_filter_sclae.Tag.ToString(), Convert.ToUInt16(Med_filter_sclae.Text, 16));
                //if (isWrite) return;
                //if (isRead) return; //判断是否在读，读的时候不进行写           
                //if (string.IsNullOrEmpty(Uv_DNR_Manual.Text)) return;
                //var txt = sender as DevExpress.XtraEditors.TextEdit;
                //if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
                //{
                //    var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Med_filter_sclae.Text);
                //    SetDnsRegister(model, Val);
                //}
            }
        }

        private void txt_Med_filter_sclae_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Med_filter_sclae_KeyDown(sender, keyEnter);
        }

        private void txt_thr_slope_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_thr_slope_man_KeyDown(sender, keyEnter);
        }

        private void txt_thr_slope_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDnsRegister(model, Val);
                    }
                }
               // SetDNRRegister(soft_thr_slope_man.Tag.ToString(), Convert.ToUInt16(soft_thr_slope_man.Text, 16));
            }
        }

        private void txt_thr_offset_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if ((null != sPortTemp && sPortTemp.IsOpen) || IsUsbOpen || m_open)
                {
                    if (isRead) return;
                    var txt = sender as DevExpress.XtraEditors.TextEdit;
                    if (string.IsNullOrEmpty(txt.Text)) return;
                    if (XChipDnr.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                    {
                        var model = XChipDnr.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                        var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                        SetDnsRegister(model, Val);
                    }
                }
                //SetDNRRegister(soft_thr_offset_man.Tag.ToString(), Convert.ToUInt16(soft_thr_offset_man.Text, 16));
            }
        }

        private void txt_thr_offset_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_thr_offset_man_KeyDown(sender, keyEnter);
        }
        private void SetTxtSlopeMan_Enable(bool isbool)
        {
            //soft_thr_slope_man.Enabled = isbool;

            //thr_slope_1x.Enabled = !isbool;
            //thr_slope_2x.Enabled = !isbool;
            //thr_slope_4x.Enabled = !isbool;
            //thr_slope_8x.Enabled = !isbool;
            //thr_slope_16x.Enabled = !isbool;
            //thr_slope_32x.Enabled = !isbool;
            //thr_slope_64x.Enabled = !isbool;
            //thr_slope_128x.Enabled = !isbool;
            //thr_slope_256x.Enabled = !isbool;          
        }
        private void SetTxtOffsetMan_Enable(bool isbool)
        {

            //soft_thr_offset_man.Enabled = isbool;

            //thr_offset_1x.Enabled = !isbool;
            //thr_offset_2x.Enabled = !isbool;
            //thr_offset_4x.Enabled = !isbool;
            //thr_offset_8x.Enabled = !isbool;
            //thr_offset_16x.Enabled = !isbool;
            //thr_offset_32x.Enabled = !isbool;
            //thr_offset_64x.Enabled = !isbool;
            //thr_offset_128x.Enabled = !isbool;
            //thr_offset_256x.Enabled = !isbool;
        }


        private void chk_thr_slope_man_CheckedChanged(object sender, EventArgs e)
        {
            //SetTxtSlopeMan_Enable(soft_thr_slope_man_en.Checked);
            if (isRead) return;
            //string dataTag = soft_thr_slope_man_en.Tag.ToString();
            //uint readVal = (uint)GetDNRRegister(dataTag);
            //if (soft_thr_slope_man_en.Checked)
            //{
            //    SetDNRRegister(dataTag, readVal | 0x01);
            //}
            //else
            //{
            //    SetDNRRegister(dataTag, readVal & 0xfe);
            //}  
            var txt = sender as CheckEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetDnsRegister(model, Val);
            }
        }

        private void chk_thr_offset_man_CheckedChanged(object sender, EventArgs e)
        {
            //SetTxtOffsetMan_Enable(soft_thr_offset_man_en.Checked);
            //if (isRead) return;
            //string dataTag = soft_thr_offset_man_en.Tag.ToString();
            //uint readVal = (uint)GetDNRRegister(dataTag);
            //if (soft_thr_offset_man_en.Checked)
            //{
            //    SetDNRRegister(dataTag, readVal | 0x02);
            //}
            //else
            //{
            //    SetDNRRegister(dataTag, readVal & 0xfd);
            //}
            if (isRead) return;
            var txt = sender as CheckEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetDnsRegister(model, Val);
            }
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            //SerialSend.isDebug = false;
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "配置文件（*.txt）|*.txt";

            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            sfd.FileName = "IQ_Setting_DNR_" + DateTime.Now.ToString("yyyyMMddHHmmss");
            if (sfd.ShowDialog() == DialogResult.OK) {
                var Pe_dal = new parameter_exportManager();
                if (null != Pe_dal) {
                    WaitBefore.Start("参数导出", "正在加载中", 60);
                    //var tempList = Pe_dal.GetList(c => c.Module == "AWB");
                    var tempList = Pe_dal.GetListToExport().Where(c => c.Module == "3D DNR"||
                    c.Module == "RAW DNR"||
                    c.Module == "YUV DNR"||
                    c.Module == "UV DNR").OrderBy(c => c.SortNum).ToList();
                    #region 判断是否需要用白天的数据
                    var Pe_patch_dal = new parameter_export_patchManager();
                    var patchList = Pe_patch_dal.GetList();
                    var conditionList = tempList.Select(c => c.ConditionAddr).Distinct();
                    foreach (var item in conditionList)
                    {
                        if (SerialSend.ReadRegister(Convert.ToUInt32(item, 16), sPortTemp) == 0)
                        {
                            patchList.RemoveAll(c => c.ConditionAddr == item);
                        }
                    }
                    #endregion
                    var sb = XChipHelp.ReadParameterExportExpand(tempList, "80", patchList, sPortTemp);
                    System.IO.File.WriteAllText(sfd.FileName, sb.ToString());
                  
                }
                WaitBefore.Close();
                DevExpress.XtraEditors.XtraMessageBox.Show("参数导出完成");
            }
        }

        private void simpleButton1_MouseEnter(object sender, EventArgs e)
        {
            isWrite = true;
        }

        private void simpleButton1_MouseLeave(object sender, EventArgs e)
        {
            isWrite = false;
        }

       
        private void lap_enhancement_Enable(bool isbool)
        {
            //soft_thr_slope_man_en.Enabled = isbool;
            //soft_thr_slope_man.Enabled = isbool;
            //thr_slope_1x.Enabled = isbool;
            //thr_slope_2x.Enabled = isbool;
            //thr_slope_4x.Enabled = isbool;
            //thr_slope_8x.Enabled = isbool;
            //thr_slope_16x.Enabled = isbool;
            //thr_slope_32x.Enabled = isbool;
            //thr_slope_64x.Enabled = isbool;
            //thr_slope_128x.Enabled = isbool;
            //thr_slope_256x.Enabled = isbool;

            //soft_thr_offset_man_en.Enabled = isbool;
            //soft_thr_offset_man.Enabled = isbool;
            //thr_offset_1x.Enabled = isbool;
            //thr_offset_2x.Enabled = isbool;
            //thr_offset_4x.Enabled = isbool;
            //thr_offset_8x.Enabled = isbool;
            //thr_offset_16x.Enabled = isbool;
            //thr_offset_32x.Enabled = isbool;
            //thr_offset_64x.Enabled = isbool;
            //thr_offset_128x.Enabled = isbool;
            //thr_offset_256x.Enabled = isbool;

            //soft_thr_en.Enabled = isbool;
            //Med_filter_sclae.Enabled = isbool;
            //Lap_enhance_scale.Enabled = isbool;
            //soft_thr_slope_man.Enabled = isbool;
            //soft_thr_offset_man.Enabled = isbool;
        }

        private void lap_enhancement_en_CheckedChanged(object sender, EventArgs e)
        {
            
            if (isRead) return;
            var txt = sender as CheckEdit;
            //lap_enhancement_Enable(txt.Checked);
            //soft_thr_Enable(txt.Checked);
            //SetTxtSlopeMan_Enable(txt.Checked);
            //SetTxtOffsetMan_Enable(txt.Checked);
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetDnsRegister(model, Val);
            }
            //if (lap_enhancement_en.CheckState == CheckState.Checked)
            //{
            //    lap_enhancement_Enable(true);
            //    if (soft_thr_en.CheckState == CheckState.Checked)
            //    {
            //        soft_thr_Enable(true);
            //        SetTxtSlopeMan_Enable(soft_thr_slope_man_en.Checked);
            //        SetTxtOffsetMan_Enable(soft_thr_offset_man_en.Checked);
            //    }
            //    else
            //    {
            //        soft_thr_Enable(false);
            //    }
                
            //}
            //else
            //{
            //    lap_enhancement_Enable(false);
            //}
        }
        private void soft_thr_Enable(bool isbool)
        {

            //thr_slope_1x.Enabled = isbool;
            //thr_slope_2x.Enabled = isbool;
            //thr_slope_4x.Enabled = isbool;
            //thr_slope_8x.Enabled = isbool;
            //thr_slope_16x.Enabled = isbool;
            //thr_slope_32x.Enabled = isbool;
            //thr_slope_64x.Enabled = isbool;
            //thr_slope_128x.Enabled = isbool;
            //thr_slope_256x.Enabled = isbool;


            //thr_offset_1x.Enabled = isbool;
            //thr_offset_2x.Enabled = isbool;
            //thr_offset_4x.Enabled = isbool;
            //thr_offset_8x.Enabled = isbool;
            //thr_offset_16x.Enabled = isbool;
            //thr_offset_32x.Enabled = isbool;
            //thr_offset_64x.Enabled = isbool;
            //thr_offset_128x.Enabled = isbool;
            //thr_offset_256x.Enabled = isbool;

        }
        private void soft_thr_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var txt = sender as CheckEdit;
            //soft_thr_Enable(txt.Checked);
            //SetTxtSlopeMan_Enable(txt.Checked);
            //SetTxtOffsetMan_Enable(txt.Checked);
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetDnsRegister(model, Val);
            }
            //if (soft_thr_en.CheckState == CheckState.Checked)
            //{
            //    soft_thr_Enable(true);
            //    SetTxtSlopeMan_Enable(soft_thr_slope_man_en.Checked);
            //    SetTxtOffsetMan_Enable(soft_thr_offset_man_en.Checked);
            //    soft_thr_offset_man_en.Enabled = true;
            //    soft_thr_slope_man_en.Enabled = true;
            //    if (soft_thr_offset_man_en.Checked)
            //    {
            //        soft_thr_offset_man.Enabled = true;
            //    }
            //    if (soft_thr_slope_man_en.Checked)
            //    {
            //        soft_thr_slope_man.Enabled = true;
            //    }

            //}
            //else
            //{
            //    soft_thr_Enable(false);
            //    soft_thr_offset_man_en.Enabled = false;
            //    soft_thr_slope_man_en.Enabled = false;
            //    soft_thr_slope_man.Enabled = false;
            //    soft_thr_offset_man.Enabled = false;
            //}
        }

        private void nr3d_wt_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            var txt = sender as CheckEdit;
            if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                //bit1置0才是勾选所以要反一下
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetDnsRegister(model, Val);
            }
            nr3d_wtEnShowHide(txt.Checked);
        }

        private void Wt_Pedestalday_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                if (isRead) return; //判断是否在读，读的时候不进行写           
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipDnr.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
                {
                    var model = XChipDnr.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetDnsRegister(model, Val);
                }
            }
        }

        private void Wt_Pedestalday_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Wt_Pedestalday_KeyDown(sender, keyEnter);
        }
        private void SetSNRRegister(addressregister model, ulong add, bool isCheck = true)
        {
            if (IsUsbOpen)
            {
                if (null != BinHelp.usbList)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));


                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }
                if (isRead) return;

                if (null != BinHelp.lspList)
                { //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), (uint)temp);
                }
            }
            SerialSend.WriteRegister(Convert.ToUInt32(model.Address, 16), add, sPortTemp);
            //if (XChipSnr.Exists(c => c.NameType == nameType))
            //{
            //    SerialSend.WriteRegister(Convert.ToUInt32(XChipSnr.Where(c => c.NameType == nameType).First().Address, 16), add, sPortTemp);
            //}
        }

        private void scale_nr_list0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtEdit.Text)) return;
                //SetSNRRegister($"scale_nr_list[{txtEdit.Tag.ToString()}]", (ulong)Convert.ToInt32(txtEdit.Text, 16));
                if (XChipSnr.Exists(c => c.NameType.ToLower() == txtEdit.Name.ToLower()))
                {
                    var model = XChipSnr.Find(c => c.NameType.ToLower() == txtEdit.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetSNRRegister(model, Val);
                }
            }
        }

        private void scale_nr_list0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_nr_list0_KeyDown(sender, keyEnter);
        }

        private void scale_sigma_list0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtEdit.Text)) return;
                //SetSNRRegister($"scale_sigma_list[{txtEdit.Tag.ToString()}]", (ulong)Convert.ToInt32(txtEdit.Text, 16));
                if (XChipSnr.Exists(c => c.NameType.ToLower() == txtEdit.Name.ToLower()))
                {
                    var model = XChipSnr.Find(c => c.NameType.ToLower() == txtEdit.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetSNRRegister(model, Val);
                }
            }
        }

        private void scale_sigma_list0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_sigma_list0_KeyDown(sender, keyEnter);
        }

        private void scale_sigma_actv0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (isWrite) return;
                var txtEdit = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txtEdit.Text)) return;
                //SetSNRRegister($"scale_sigma_actv[{txtEdit.Tag.ToString()}]", (ulong)Convert.ToInt32(txtEdit.Text, 16));
                if (XChipSnr.Exists(c => c.NameType.ToLower() == txtEdit.Name.ToLower()))
                {
                    var model = XChipSnr.Find(c => c.NameType.ToLower() == txtEdit.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetSNRRegister(model, Val);
                }
            }
        }

        private void scale_sigma_actv0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_sigma_actv0_KeyDown(sender, keyEnter);
        }

        private void tunning_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;

            if (XChipSnr.Exists(c => c.NameType.ToLower() == $"{tunning_en.Name}".ToLower()))
            {
                var model = XChipSnr.Find(c => c.NameType.ToLower() == $"{tunning_en.Name}".ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), tunning_en.Checked);
                SetSNRRegister(model, Val);
            }

            Mode_switch.Enabled = tunning_en.Checked;
            Mode_switch.SelectedIndex = 0;
        }

        private void Mode_switch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isWrite) return;

            if (XChipSnr.Exists(c => c.NameType.ToLower() == "tunning_branch".ToLower()))
            {
                var model = XChipSnr.Find(c => c.NameType.ToLower() == "tunning_branch".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Mode_switch.SelectedIndex.ToString());
                SetSNRRegister(model, Val);
            }
            if (XChipSnr.Exists(c => c.NameType.ToLower() == $"{tunning_en.Name}".ToLower()))
            {
                var model = XChipSnr.Find(c => c.NameType.ToLower() == $"{tunning_en.Name}".ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), tunning_en.Checked);
                SetSNRRegister(model, Val);
            }
        }
        private void nr3d_wtEnShowHide(bool isBool)
        {
            Wt_Pedestalday.Enabled = isBool;
            Wt_Pedestalnight.Enabled = isBool;
            Wt_Pedestalmidnight.Enabled = isBool;
            Max_Move_Px_Wtday.Enabled = isBool;
            Max_Move_Px_Wtnight.Enabled = isBool;
            Max_Move_Px_Wtmidnight.Enabled = isBool;
            Wt_Pedestal.Enabled = !isBool;
            Max_Move_Px_Wt.Enabled = !isBool;
        }

        private void scale_sigma_man_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            //var readVal = GetSNRRegister("scale_sigma_man_en");
            //SetSNRRegister("scale_sigma_man_en", (ulong)(scale_sigma_man_en.Checked ? (readVal | 0x01) : (readVal & 0xfe)));
            if (XChipSnr.Exists(c => c.NameType.ToLower() == $"scale_sigma_man_en".ToLower()))
            {
                var model = XChipSnr.Find(c => c.NameType.ToLower() == $"scale_sigma_man_en".ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), scale_sigma_man_en.Checked);
                SetSNRRegister(model, Val);
            }
            EnShowHide();
        }

        private void scale_nr_man_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            //var readVal = GetSNRRegister("scale_nr_man_en");
            //SetSNRRegister("scale_nr_man_en", (ulong)(scale_nr_man_en.Checked ? (readVal | 0x01) : (readVal & 0xfe)));
            if (XChipSnr.Exists(c => c.NameType.ToLower() == $"scale_nr_man_en".ToLower()))
            {
                var model = XChipSnr.Find(c => c.NameType.ToLower() == $"scale_nr_man_en".ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), scale_nr_man_en.Checked);
                SetSNRRegister(model, Val);
            }
            EnShowHide();
        }
        private void EnShowHide()
        {
            scale_sigma_man.Enabled = scale_sigma_man_en.Checked;
            scale_nr_man.Enabled = scale_nr_man_en.Checked;
            scale_sigma_list0.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list1.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list2.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list3.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list4.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list5.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list6.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list7.Enabled = !scale_sigma_man_en.Checked;
            scale_sigma_list8.Enabled = !scale_sigma_man_en.Checked;

            scale_nr_list0.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list1.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list2.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list3.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list4.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list5.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list6.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list7.Enabled = !scale_nr_man_en.Checked;
            scale_nr_list8.Enabled = !scale_nr_man_en.Checked;
        }

        private void scale_sigma_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(scale_sigma_man.Text))
            {
                if (XChipSnr.Exists(c => c.NameType.ToLower() == $"scale_sigma_man".ToLower()))
                {
                    var model = XChipSnr.Find(c => c.NameType.ToLower() == $"scale_sigma_man".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_sigma_man.Text);
                    SetSNRRegister(model, Val);
                }
            }
        }

        private void scale_sigma_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_sigma_man_KeyDown(sender, keyEnter);
        }

        private void scale_nr_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            scale_nr_man_KeyDown(sender, keyEnter);
        }

        private void scale_nr_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(scale_nr_man.Text))
            {
                //var nHigh = Convert.ToUInt32(scale_nr_man.Text, 16);
                //SetSNRRegister("scale_nr_man", (ulong)nHigh & 0xff);
                if (XChipSnr.Exists(c => c.NameType.ToLower() == $"scale_nr_man".ToLower()))
                {
                    var model = XChipSnr.Find(c => c.NameType.ToLower() == $"scale_nr_man".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_nr_man.Text);
                    SetSNRRegister(model, Val);
                }
            }
        }
    }
}
